using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameOnCopyBook.Rendering
{
    /// <summary>
    /// Questo è un componente del gioco che implementa IUpdateable.
    /// </summary>
    public class Rendering : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        RenderingData renderingData = new RenderingData();
        
        public Rendering(Game game)
            : base(game)
        {
            // TODO: creare qui eventuali componenti figlio
            Game.Services.AddService(typeof(RenderingData), renderingData);
        }

        /// <summary>
        /// Consente al componente del gioco di eseguire l'inizializzazione necessaria prima dell'inizio
        /// dell'esecuzione. Qui è possibile eseguire query per eventuali servizi necessari e caricare contenuto.
        /// </summary>
        public override void Initialize()
        {
            // TODO: aggiungere qui il codice di inizializzazione
            base.Initialize();
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(RenderingData));
            base.Dispose(disposing);
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            renderingData.font = Game.Content.Load<SpriteFont>("SpriteFont1");
            renderingData.font2 = Game.Content.Load<SpriteFont>("SpriteFont2");
            renderingData.backgroundTexture = Game.Content.Load<Texture2D>(@"Textures\background1376x768");
            renderingData.playerTexture = Game.Content.Load<Texture2D>(@"Textures\pentagon0");
            renderingData.enemy0Texture = Game.Content.Load<Texture2D>(@"Textures\square0");
            renderingData.enemy1Texture = Game.Content.Load<Texture2D>(@"Textures\triangle00");
            renderingData.enemy2Texture = Game.Content.Load<Texture2D>(@"Textures\line0");
            renderingData.enemy3Texture = Game.Content.Load<Texture2D>(@"Textures\point0");
            renderingData.homeTexture = Game.Content.Load<Texture2D>(@"Textures\home");
            renderingData.pauseTexture = Game.Content.Load<Texture2D>(@"Textures\background3");
            renderingData.projectileTexture = Game.Content.Load<Texture2D>(@"Textures\bullet0");
            renderingData.superProjectileTexture = Game.Content.Load<Texture2D>(@"Textures\superBullet0");
            renderingData.headerTexture = Game.Content.Load<Texture2D>(@"Textures\header1376");
            renderingData.bboxProj = Game.Content.Load<Texture2D>(@"Textures\bboxProj");
            renderingData.bboxTriangle = Game.Content.Load<Texture2D>(@"Textures\bboxTriangle");
            renderingData.bboxLine = Game.Content.Load<Texture2D>(@"Textures\bboxLine");
            renderingData.bboxPoint = Game.Content.Load<Texture2D>(@"Textures\bboxPoint");
            renderingData.bboxSquare = Game.Content.Load<Texture2D>(@"Textures\square");
     
            
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            
            spriteBatch.Begin();
            
            
            spriteBatch.Draw(renderingData.backgroundTexture, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
            if (GameLogic.game_state.Level == "Tutorial")
            {
                
                foreach (var p in GameLogic.game_state.Player.Projectiles.Value)
                {

                    if (p.Type == 1)
                    {
                        // debug bounding box projectile
                        //spriteBatch.Draw(renderingData.bboxProj, new Vector2(p.BBox.Min.X, p.BBox.Min.Y), null, Color.Green, p.Rotation, new Vector2(renderingData.bboxProj.Width / 2.0f, renderingData.bboxProj.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                        spriteBatch.Draw(renderingData.projectileTexture, p.Position.Value, null, Color.Red, p.Rotation, new Vector2(renderingData.projectileTexture.Width / 2.0f, renderingData.projectileTexture.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                    }
                    if (p.Type == 2)
                    {
                        spriteBatch.Draw(renderingData.superProjectileTexture, p.Position.Value, null, Color.Red, p.Rotation, new Vector2(renderingData.superProjectileTexture.Width / 2.0f, renderingData.superProjectileTexture.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                    }
                }

                spriteBatch.Draw(renderingData.playerTexture, GameLogic.game_state.Player.Position.Value, null, Color.White, GameLogic.game_state.Player.DirectionFire.Value + MathHelper.PiOver2,
                new Vector2(renderingData.playerTexture.Width / 2.0f, renderingData.playerTexture.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.DrawString(renderingData.font2, "TUTORIAL", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f - 180.0f, 70.0f), Color.Black, 0.0f, renderingData.font.MeasureString("TUTORIAL") / 2.0f, 1.2f, SpriteEffects.None, 0.5f);
                spriteBatch.DrawString(renderingData.font, " Move with W A S D ", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 250.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" Move with W A S D ") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
                spriteBatch.DrawString(renderingData.font, " Aim with Mouse  ", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 550.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" Aim with Mouse  ") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
                spriteBatch.DrawString(renderingData.font, " Base Shot click LeftButton ", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 650.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" Base Shot click LeftButton ") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
                spriteBatch.DrawString(renderingData.font, " Super Shot click and release RightButton and wait 1 sec ", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 700.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" Super Shot click and release RightButton and wait 1 sec ") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
                if (GameLogic.game_state.Timer >= -5)
                {
                    spriteBatch.DrawString(renderingData.font2,"" + -GameLogic.game_state.Timer + " ", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 330.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" ") / 2.0f, 1.4f, SpriteEffects.None, 0.5f);
                }
            }
            else
            {
                foreach (var e in GameLogic.game_state.Enemies.Value)
                {


                    if (e.Type == 0)
                    {
                        // debug bounding box square
                        //spriteBatch.Draw(renderingData.bboxSquare, new Vector2(e.BBox.Min.X, e.BBox.Min.Y), null, Color.Red,
                        //0.0f, new Vector2(renderingData.bboxSquare.Width, renderingData.bboxSquare.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);
                        spriteBatch.Draw(renderingData.enemy0Texture, e.Position.Value - new Vector2(renderingData.enemy0Texture.Width, renderingData.enemy0Texture.Height) * 0.5f, null, Color.White,
                        e.Heading, new Vector2(renderingData.enemy0Texture.Width, renderingData.enemy0Texture.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);

                    }
                    if (e.Type == 1)
                    {
                        // debug bounding box triangle
                        //spriteBatch.Draw(renderingData.bboxTriangle, new Vector2(e.BBox.Min.X, e.BBox.Min.Y), null, Color.Red,
                        //e.Heading, new Vector2(renderingData.bboxTriangle.Width, renderingData.bboxTriangle.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);
                        spriteBatch.Draw(renderingData.enemy1Texture, e.Position.Value - new Vector2(renderingData.enemy1Texture.Width, renderingData.enemy1Texture.Height) * 0.5f, null, Color.White,
                        e.Heading, new Vector2(renderingData.enemy1Texture.Width, renderingData.enemy1Texture.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);

                    }
                    if (e.Type == 2)
                    {
                        // debug bounding box line
                        //spriteBatch.Draw(renderingData.bboxLine, new Vector2(e.BBox.Min.X, e.BBox.Min.Y), null, Color.Red,
                        //e.Heading, new Vector2(renderingData.bboxLine.Width, renderingData.bboxLine.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);
                        spriteBatch.Draw(renderingData.enemy2Texture, e.Position.Value - new Vector2(renderingData.enemy2Texture.Width, renderingData.enemy2Texture.Height) * 0.5f, null, Color.White,
                        e.Heading, new Vector2(renderingData.enemy2Texture.Width, renderingData.enemy2Texture.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);

                    }
                    if (e.Type == 3)
                    {
                        // debug bounding box point
                        //spriteBatch.Draw(renderingData.bboxPoint, new Vector2(e.BBox.Min.X, e.BBox.Min.Y), null, Color.Red,
                        //e.Heading, new Vector2(renderingData.bboxPoint.Width, renderingData.bboxPoint.Height) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);
                        spriteBatch.Draw(renderingData.enemy3Texture, e.Position.Value - new Vector2(renderingData.enemy3Texture.Width, renderingData.enemy3Texture.Height) * 0.5f, null, Color.White,
                        e.Heading, new Vector2(renderingData.enemy3Texture.Width, renderingData.enemy3Texture.Height) * 0.5f, 1.5f, SpriteEffects.None, 0.0f);

                    }

                }

                foreach (var p in GameLogic.game_state.Player.Projectiles.Value)
                {

                    if (p.Type == 1)
                    {
                        // debug bounding box projectile
                        //spriteBatch.Draw(renderingData.bboxProj, new Vector2(p.BBox.Min.X, p.BBox.Min.Y), null, Color.Green, p.Rotation, new Vector2(renderingData.bboxProj.Width / 2.0f, renderingData.bboxProj.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                        spriteBatch.Draw(renderingData.projectileTexture, p.Position.Value, null, Color.Red, p.Rotation, new Vector2(renderingData.projectileTexture.Width / 2.0f, renderingData.projectileTexture.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                    }
                    if (p.Type == 2)
                    {
                        spriteBatch.Draw(renderingData.superProjectileTexture, p.Position.Value, null, Color.Red, p.Rotation, new Vector2(renderingData.superProjectileTexture.Width / 2.0f, renderingData.superProjectileTexture.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                    }
                }


                // debug bounding box pentagon
                //spriteBatch.Draw(renderingData.enemy0Texture, new Vector2(GameLogic.game_state.Player.BBox.Min.X, GameLogic.game_state.Player.BBox.Min.Y), null, Color.Red, GameLogic.game_state.Player.DirectionFire.Value + MathHelper.PiOver2,
                //    new Vector2(renderingData.enemy0Texture.Width / 2.0f, renderingData.enemy0Texture.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
                spriteBatch.Draw(renderingData.headerTexture, new Rectangle(0, 0, Game.GraphicsDevice.Viewport.Width, renderingData.headerTexture.Height), Color.White);
                spriteBatch.DrawString(renderingData.font, "Level: " + GameLogic.game_state.Level, new Vector2(250.0f, 60.0f), Color.Black);
                spriteBatch.DrawString(renderingData.font, "Time: " + GameLogic.game_state.Timer + " sec", new Vector2(850.0f, 60.0f), Color.Black);
                spriteBatch.Draw(renderingData.playerTexture, GameLogic.game_state.Player.Position.Value, null, Color.White, GameLogic.game_state.Player.DirectionFire.Value + MathHelper.PiOver2,
                    new Vector2(renderingData.playerTexture.Width / 2.0f, renderingData.playerTexture.Height / 2.0f), 1.0f, SpriteEffects.None, 0.0f);
          

            }
            
            spriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
